clear all
set maxvar 30000
set more off
tempfile ubigeo temp


/*

This dofile compiles the random allocations of exposure to violence.

THis if only for government and terrorist violence

*/

** PATHS
local working ../working

** DATA
* A
use `working'/attachviolenceAri1, clear
local t A
forvalues p = 1/200 {
	merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolenceAri`p'
	drop _merge
	egen preesc`t'`p' = rowtotal(events`t'ri1_`p' events`t'ri2_`p' events`t'ri3_`p' events`t'ri4_`p' events`t'ri5_`p' events`t'ri6_`p')
	egen primar`t'`p' = rowtotal(events`t'ri7_`p' events`t'ri8_`p' events`t'ri9_`p' events`t'ri10_`p' events`t'ri11_`p')
	egen second`t'`p' = rowtotal(events`t'ri12_`p' events`t'ri13_`p' events`t'ri14_`p' events`t'ri15_`p' events`t'ri16_`p' events`t'ri17_`p' events`t'ri18_`p')
	egen tertia`t'`p' = rowtotal(events`t'ri19_`p' events`t'ri20_`p' events`t'ri21_`p' events`t'ri22_`p' events`t'ri23_`p' events`t'ri24_`p' events`t'ri25_`p' events`t'ri26_`p' events`t'ri27_`p' events`t'ri28_`p' events`t'ri29_`p' events`t'ri30_`p')

	gen todo`t'`p' = preesc`t'`p' + primar`t'`p' + second`t'`p' + tertia`t'`p'	
}	

keep conglome vivienda hogar codperso year preesc* primar* second* tertia* todo*
save `working'/A, replace

* T
local t T
use `working'/attachviolence`t'ri1, clear
forvalues p = 1/200 {
	merge 1:1 conglome vivienda hogar codperso year using `working'/attachviolence`t'ri`p'
	drop _merge
	egen preesc`t'`p' = rowtotal(events`t'ri1_`p' events`t'ri2_`p' events`t'ri3_`p' events`t'ri4_`p' events`t'ri5_`p' events`t'ri6_`p')
	egen primar`t'`p' = rowtotal(events`t'ri7_`p' events`t'ri8_`p' events`t'ri9_`p' events`t'ri10_`p' events`t'ri11_`p')
	egen second`t'`p' = rowtotal(events`t'ri12_`p' events`t'ri13_`p' events`t'ri14_`p' events`t'ri15_`p' events`t'ri16_`p' events`t'ri17_`p' events`t'ri18_`p')
	egen tertia`t'`p' = rowtotal(events`t'ri19_`p' events`t'ri20_`p' events`t'ri21_`p' events`t'ri22_`p' events`t'ri23_`p' events`t'ri24_`p' events`t'ri25_`p' events`t'ri26_`p' events`t'ri27_`p' events`t'ri28_`p' events`t'ri29_`p' events`t'ri30_`p')

	gen todo`t'`p' = preesc`t'`p' + primar`t'`p' + second`t'`p' + tertia`t'`p'	
}	

keep conglome vivienda hogar codperso year preesc* primar* second* tertia* todo*
save `working'/`t', replace



* COMPILE
use `working'/A
merge 1:1 conglome vivienda hogar codperso year using `working'/T
drop _merge


* SAVE
compress
save `working'/ri, replace

erase `working'/A.dta
erase `working'/T.dta


